// Learn cc.Class:
//  - [Chinese] https://docs.cocos.com/creator/manual/zh/scripting/class.html
//  - [English] http://docs.cocos2d-x.org/creator/manual/en/scripting/class.html
// Learn Attribute:
//  - [Chinese] https://docs.cocos.com/creator/manual/zh/scripting/reference/attributes.html
//  - [English] http://docs.cocos2d-x.org/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - [Chinese] https://docs.cocos.com/creator/manual/zh/scripting/life-cycle-callbacks.html
//  - [English] https://www.cocos2d-x.org/docs/creator/manual/en/scripting/life-cycle-callbacks.html
var GameData=require("GameData");

cc.Class({
    extends: cc.Component,

    properties: {
        // foo: {
        //     // ATTRIBUTES:
        //     default: null,        // The default value will be used only when the component attaching
        //                           // to a node for the first time
        //     type: cc.SpriteFrame, // optional, default is typeof default
        //     serializable: true,   // optional, default is true
        // },
        // bar: {
        //     get () {
        //         return this._bar;
        //     },
        //     set (value) {
        //         this._bar = value;
        //     }
        // },

        mPrefab:{
            default:null,
            type:cc.Prefab
        },
        mFishPool:{
            default:null,
            type:cc.Node
        },
        mHook:{
            default:null,
            type:cc.Node
        },
        mDepth:{
            default:0,
            type:cc.Float,
            visible:false
        },
        mSceneData:{
            default:null,
            type:cc.JsonAsset
        },
        mIndex:{
            default:0,
            visible:false
        },
        mController:{
            default:null,
            type:cc.Node
        }
    },

    // LIFE-CYCLE CALLBACKS:

     onLoad () {
         var manager=cc.director.getCollisionManager();
         manager.enabled=true;
         manager.enabledDebugDraw=true;
     },
    
    start () {
        //cc.log(" start");
    },
    reset(){
        GameData.instance.depth=0;
        GameData.instance.score=0;

        this.mFishPool.removeAllChildren(true);
        this.mController.removeAllChildren(true);

        cc.find("Hook/Camera/MainUI/RankButton",this.node).active=false;
        cc.find("Hook/Camera/MainUI/reset_Game",this.node).active=false;
        this.node.getChildByName("Hook").getComponent("Hook").startLine();
        cc.log("restart Game");
        this.mIndex=0;

    },
    update (dt) {
        this.mDepth=Math.floor(Math.abs(this.mHook.y)/100); //100 pixel=1meter


        let data=this.mSceneData.json[this.mIndex];
        if(data!=undefined && this.mDepth>=data.depth)  //every meter instantiate a fish
        {
            var fish=cc.instantiate(this.mPrefab);
            cc.loader.loadRes(data.res,cc.SpriteFrame,function(err,spriteFrame){
                //get the res
                //cc.log(err);
                if(!err){
                    //cc.log("change picture");
                    fish.getComponent(cc.Sprite).spriteFrame=spriteFrame;
                }
            });
            fish.x=Math.random()*640-320;   //(-320-->320)
            fish.y=this.mHook.y-480-100;
            this.mFishPool.addChild(fish);

            this.mIndex++;
        }
    },
});